package arrays;

/**
 * @author LiangMu
 * @version \$Id: P12.java, v 0.1, 2022年12月14日 13:06 LiangMu Exp $$
 */
public class P12 {
    public static void main(String[] args) {
        int[] ints = new int[]{1, 3, 5, 6};
        int i = searchInsert(ints, 2);
        System.out.println(i);
    }

    public static int searchInsert(int[] nums, int target) {
        int left = 0, right = nums.length;
        if (nums[nums.length - 1] < target) return nums.length;
        while (left <= right) {
            int index = (left + right) / 2;
            if (nums[index] == target) {
                return index;
            }
            if (nums[index] > target) {
                right = index - 1;
            }
            if (nums[index] < target) {
                left = index + 1;
            }
        }
        return nums[left] > target ? left : left + 1;
    }
}
